<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="供应商编号" prop="supplierNum">
      <el-input v-model="dataForm.supplierNum" placeholder="供应商编号"></el-input>
    </el-form-item>
    <el-form-item label="供应商证书编号" prop="certificate">
      <el-input v-model="dataForm.certificate" placeholder="供应商证书编号"></el-input>
    </el-form-item>
    <el-form-item label="公司名称" prop="company">
      <el-input v-model="dataForm.company" placeholder="公司名称"></el-input>
    </el-form-item>
    <el-form-item label="公司性质" prop="kind">
      <el-input v-model="dataForm.kind" placeholder="公司性质"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="contact">
      <el-input v-model="dataForm.contact" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
    </el-form-item>
    <el-form-item label="传真" prop="fax">
      <el-input v-model="dataForm.fax" placeholder="传真"></el-input>
    </el-form-item>
    <el-form-item label="电子邮件" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮件"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="zip">
      <el-input v-model="dataForm.zip" placeholder="邮编"></el-input>
    </el-form-item>
    <el-form-item label="注册资金" prop="regFund">
      <el-input v-model="dataForm.regFund" placeholder="注册资金"></el-input>
    </el-form-item>
    <el-form-item label="法人代表" prop="corporation">
      <el-input v-model="dataForm.corporation" placeholder="法人代表"></el-input>
    </el-form-item>
    <el-form-item label="业绩" prop="achievement">
      <el-input v-model="dataForm.achievement" placeholder="业绩"></el-input>
    </el-form-item>
    <el-form-item label="是否可用" prop="status">
      <el-input v-model="dataForm.status" placeholder="是否可用"></el-input>
    </el-form-item>
    <el-form-item label="用户序号" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户序号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          supplierNum: '',
          certificate: '',
          company: '',
          kind: '',
          contact: '',
          phone: '',
          fax: '',
          email: '',
          address: '',
          zip: '',
          regFund: '',
          corporation: '',
          achievement: '',
          status: '',
          userId: ''
        },
        dataRule: {
          supplierNum: [
            { required: true, message: '供应商编号不能为空', trigger: 'blur' }
          ],
          certificate: [
            { required: true, message: '供应商证书编号不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '公司名称不能为空', trigger: 'blur' }
          ],
          kind: [
            { required: true, message: '公司性质不能为空', trigger: 'blur' }
          ],
          contact: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话不能为空', trigger: 'blur' }
          ],
          fax: [
            { required: true, message: '传真不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮件不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          zip: [
            { required: true, message: '邮编不能为空', trigger: 'blur' }
          ],
          regFund: [
            { required: true, message: '注册资金不能为空', trigger: 'blur' }
          ],
          corporation: [
            { required: true, message: '法人代表不能为空', trigger: 'blur' }
          ],
          achievement: [
            { required: true, message: '业绩不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户序号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/supplier/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.supplierNum = data.supplier.supplierNum
                this.dataForm.certificate = data.supplier.certificate
                this.dataForm.company = data.supplier.company
                this.dataForm.kind = data.supplier.kind
                this.dataForm.contact = data.supplier.contact
                this.dataForm.phone = data.supplier.phone
                this.dataForm.fax = data.supplier.fax
                this.dataForm.email = data.supplier.email
                this.dataForm.address = data.supplier.address
                this.dataForm.zip = data.supplier.zip
                this.dataForm.regFund = data.supplier.regFund
                this.dataForm.corporation = data.supplier.corporation
                this.dataForm.achievement = data.supplier.achievement
                this.dataForm.status = data.supplier.status
                this.dataForm.userId = data.supplier.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/supplier/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'supplierNum': this.dataForm.supplierNum,
                'certificate': this.dataForm.certificate,
                'company': this.dataForm.company,
                'kind': this.dataForm.kind,
                'contact': this.dataForm.contact,
                'phone': this.dataForm.phone,
                'fax': this.dataForm.fax,
                'email': this.dataForm.email,
                'address': this.dataForm.address,
                'zip': this.dataForm.zip,
                'regFund': this.dataForm.regFund,
                'corporation': this.dataForm.corporation,
                'achievement': this.dataForm.achievement,
                'status': this.dataForm.status,
                'userId': this.dataForm.userId
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
